package com.ruoyi.web.controller.admin.construction;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.dto.StageAddDTO;
import com.ruoyi.system.dto.StageQueryDTO;
import com.ruoyi.system.dto.StageUpdateDTO;
import com.ruoyi.system.entity.ConstructionProjectDO;
import com.ruoyi.system.entity.ConstructionStageDO;
import com.ruoyi.system.service.ConstructionStageService;
import com.ruoyi.system.service.IProjectStageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 施工阶段配置Controller（工程管理-项目施工-施工阶段配置）
 */
@RestController
@RequestMapping("/admin/construction/stage")
@Api(tags = "施工阶段配置管理")
public class ConstructionStageController {

    @Resource
    private ConstructionStageService stageService;

    @Resource
    private IProjectStageService projectStageService;

    /**
     * 新增自定义施工阶段
     */
    @PostMapping("/add-custom")
    @ApiOperation("新增自定义施工阶段")
    public AjaxResult addCustomStage(
            @Validated @RequestBody StageAddDTO dto
    ) {
        // 获取当前设计师ID（自定义阶段仅限当前设计师使用，需结合权限框架）
        Long userId = SecurityUtils.getUserId();
        Long stageId = stageService.addCustomStage(dto, userId.toString(), userId.toString());
        return AjaxResult.success("自定义阶段创建成功", stageId);
    }

    /**
     * 修改施工阶段
     */
    @PostMapping("/update")
    @ApiOperation("修改施工阶段")
    public AjaxResult updateStage(
            @Validated @RequestBody StageUpdateDTO dto
    ) {
        if (dto.getStageId() == null) {
            return AjaxResult.error("阶段ID不能为空");
        }
        Long userId = SecurityUtils.getUserId();
        stageService.updateStage(dto, userId.toString());
        return AjaxResult.success("施工阶段修改成功");
    }

    /**
     * 删除自定义施工阶段
     */
    @PostMapping("/delete/{stageId}")
    @ApiOperation("删除自定义施工阶段")
    public AjaxResult deleteCustomStage(@PathVariable Long stageId) {
        stageService.deleteCustomStage(stageId);
        return AjaxResult.success("自定义阶段删除成功");
    }

    /**
     * 分页查询施工阶段列表
     */
    @GetMapping("/page")
    public AjaxResult queryStagePage(StageQueryDTO queryDTO) {
        // 自定义阶段仅查询当前设计师的，固定阶段全部可见
        queryDTO.setDesignerId("103");
        Page<ConstructionStageDO> pageResult = stageService.queryStagePage(queryDTO);
        return AjaxResult.success(pageResult);
    }

    /**
     * 根据id查询施工阶段详情
     */
    @GetMapping("/{stageId}")
    public AjaxResult getStageById(@PathVariable Long stageId) {
        ConstructionStageDO stage = stageService.getById(stageId);
        if (stage == null) {
            return AjaxResult.error("阶段不存在");
        }
        return AjaxResult.success(stage);
    }

    /**
     * 批量绑定阶段到项目
     */
    @PostMapping("/batch-bind")
    public AjaxResult batchBindProjects(
            @RequestParam List<Long> stageIdList,
            @RequestParam List<Long> projectIdList
    ) {
        Long userId = SecurityUtils.getUserId();
        stageService.batchBindProjects(stageIdList, projectIdList, userId.toString());
        return AjaxResult.success("阶段与项目绑定成功");
    }

    /**
     * 批量解除阶段与项目的绑定
     */
    @PostMapping("/batch-unbind")
    public AjaxResult batchUnbindProjects(
            @RequestParam List<Long> stageIdList,
            @RequestParam List<Long> projectIdList
    ) {
        stageService.batchUnbindProjects(stageIdList, projectIdList);
        return AjaxResult.success("阶段与项目解除绑定成功");
    }

    /**
     * 根据阶段id查询所有未绑定阶段的项目
     */
    @GetMapping("/projects/{stageId}")
    public AjaxResult getProjectsByStageId(@PathVariable Long stageId) {
        List<ConstructionProjectDO> projects = projectStageService.getProjectsByStageId(stageId);
        return AjaxResult.success(projects);
    }

    /**
     * 根据阶段id查询所有绑定阶段的项目
     */
    @GetMapping("/unbound-projects/{stageId}")
    public AjaxResult getUnboundProjectsByStageId(@PathVariable Long stageId) {
        List<ConstructionProjectDO> projects = projectStageService.getUnlinkedProjectsByStageId(stageId);
        return AjaxResult.success(projects);
    }
}